package com.parktech.sun.domain.service.sunhz.impl;

import com.parktech.sun.domain.model.sunhz.Enterprise;
import com.parktech.sun.domain.model.sunhz.EnterpriseCategoryProduct;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.sunhz.EnterpriseRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.sunhz.EnterpriseService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-3
 * Time: 下午1:42
 */
@Service("enterpriseService")
public class EnterpriseServiceImpl extends BaseServiceImpl<Enterprise> implements EnterpriseService {

    private EnterpriseRepository enterpriseRepository;

    @Autowired
    public EnterpriseServiceImpl(EnterpriseRepository enterpriseRepository) {
        super(enterpriseRepository);
        this.enterpriseRepository = enterpriseRepository;
    }

    public Page<Enterprise> search(String name, int category, int publishStatus, Page page) {

        Criteria criteria = enterpriseRepository.createCriteria();
        if (StringUtils.isNotEmpty(name)) {
            criteria.add(Property.forName("title").like(name, MatchMode.ANYWHERE));
        }
        if (category != -1) {
            criteria.add(Restrictions.sqlRestriction("{alias}.categories & " + category + " = " + category));
        }
        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return enterpriseRepository.page(criteria, page.getPageNo(), page.getPageSize());

//        return ret;
    }

    public Page searchCategoryProduct(String name, Page page) {

        Criteria criteria = enterpriseRepository.createCriteria(EnterpriseCategoryProduct.class);
        if (StringUtils.isNotEmpty(name)) {
            criteria.add(Property.forName("name").like(name, MatchMode.ANYWHERE));
        }
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("modifyTime"));
        return enterpriseRepository.page(criteria, page.getPageNo(), page.getPageSize());
    }

    public void saveOrUpdateCategoryProduct(EnterpriseCategoryProduct enterpriseCategoryProduct) {
        enterpriseRepository.saveOrUpdateCategoryProduct(enterpriseCategoryProduct);
    }

    public EnterpriseCategoryProduct getProductById(Long productId) {
        return enterpriseRepository.getProductById(productId);
    }

    public List<EnterpriseCategoryProduct> listProductByCategory(Long category) {
        Criteria criteria = enterpriseRepository.createCriteria(EnterpriseCategoryProduct.class);
        criteria.add(Property.forName("category").eq(category));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("modifyTime"));
        return criteria.list();
    }

    public Page pageBy(Page page) {
        Criteria criteria = enterpriseRepository.createCriteria();
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        return enterpriseRepository.page(criteria, page.getPageNo(), page.getPageSize());
    }

    public List<Enterprise> listTopRecommendEnterprise(long category, int maxResult) {
        Criteria criteria = enterpriseRepository.createCriteria();
        criteria.add(Restrictions.sqlRestriction("{alias}.categories & " + category + " = " + category));
        criteria.add(Property.forName("member").eq(true));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        criteria.setMaxResults(maxResult);
        return criteria.list();
    }

    public List<Enterprise> listByCategory(int category) {
        Criteria criteria = enterpriseRepository.createCriteria();
        criteria.add(Restrictions.sqlRestriction("{alias}.categories & " + category + " = " + category));
//        criteria.add(Property.forName("member").eq(true));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        return criteria.list();
    }

    public boolean checkMemberCode(String memberCode) {
        Criteria criteria = enterpriseRepository.createCriteria();
//        criteria.add(Property.forName("member").eq(true));
        criteria.add(Property.forName("memberCode").eq(memberCode));
        List list = criteria.list();
        return list != null && list.size() > 0;
    }
}
